/**
 * 
 */
var _url = "service.do?s=userinfo";
var $dialog;

$(function(){
	init();
});

var init = function() {
	$dialog = $("#newActivity").clone();
	$("#accordion").accordion({active:0});
	getUserInfo();
	getActivityInfo();
	$("#addActivity").bind("click", addActivity);
	$("#btnSave").bind("click", saveActivity);
	$("#activityName").bind("propertychange", onActivityInfoChange);
	$("#activityUrl").bind("propertychange", onActivityInfoChange);
	$("#btnSave").attr("disabled", true);
};

var getUserInfo = function() {
	
};

var getActivityInfo = function() {
	$.post(_url,{op:"getActivityInfo"},function(event){
		$("#activitySelect").empty();
		if(event.returnCode == "success"){
			$.each(event.result.activityInfoList, function(index, obj){
				$("#activitySelect").get(0).options.add(new Option(obj.activityName,obj.activityId));
				$("#activitySelect option:eq("+index+")").data("option-activity",obj);
			});
		} else if(event.errorMessage != null && event.errorMessage != "") {
			alert(event.errorMessage);
		} else {
			alert("获取活动信息失败");
		}
	},"json");
	$("#activitySelect").bind("dblclick", function(){
		var data = $("#activitySelect option:selected").data("option-activity");
		$("#activityName").val(data.activityName);
		$("#activityUrl").val(data.activityUrl);
		$("#activityName").data("option-activity",data);
		});
};

var addActivity = function() {
	$dialog.clone().dialog({
		modal: true,
        autoOpen: true,
        width: 330,
        height: 180,
        title: "新建活动",
//        show: true,
//        hide: true,
        buttons: {
            "确定": function() {
			  	var activityName = $(this).find("#activityName").val();
				var activityUrl = $(this).find("#activityUrl").val();
				if(activityName == "") {
					alert("请输入活动名！");
					return;
				}
				var $this = $(this);
				$.post(_url,{
					op:"addActivity",
					"activityName":activityName,
					"activityUrl":activityUrl
				},function(event){
					if(event.returnCode == "success"){
						getActivityInfo();
						$this.dialog("destroy");
					} else if(event.errorMessage != null && event.errorMessage != "") {
						alert(event.errorMessage);
					} else {
						alert("新建活动失败");
					}
				},"json");
            },
            "取消": function() {
            	$(this).dialog('destroy');
            	return false;
            }
        }
	});
}

var saveActivity = function() {
	var activityId = $("#activityName").data("option-activity").activityId;
	var activityName = $("#activityName").val();
	var activityUrl = $("#activityUrl").val();
	if(activityName == "") {
		alert("请输入活动名！");
		return;
	}
	var data = {
		op:"saveActivity",
		"activityId":activityId,
		"activityName":activityName,
		"activityUrl":activityUrl
	};
	$.post(_url,data,function(event){
		if(event.returnCode == "success"){
			getActivityInfo();
			$("#btnSave").attr("disabled", true);
		} else if(event.errorMessage != null && event.errorMessage != "") {
			alert(event.errorMessage);
		} else {
			alert("修改活动信息失败");
		}
	},"json");
}

var onActivityInfoChange = function(){
	$("#btnSave").attr("disabled", false);
}
